package com.xyazm.fulfill.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.xyazm.fulfill.domain.param.TriggerFulFillParam;
import com.xyazm.fulfill.service.IFulfillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 接受订单履约消息 ok
 * @author xuyou
 * @date 2022年08月15日 22:42
 */
@Slf4j
@Component
public class TriggerOrderFulfillTopicListener implements MessageListenerConcurrently {

    /**
     * 履约服务
     */
    @Autowired
    private IFulfillService fulfillService;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {
            for(MessageExt messageExt : list) {
                String message = new String(messageExt.getBody());
                log.info("接受订单履约 request={}", message);
                TriggerFulFillParam param = JSON.parseObject(message, TriggerFulFillParam.class);
                fulfillService.triggerFulFill(param);
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            log.error("consumer error", e);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    }
}
